//
//  DDShareViewController.m
//  dingdang
//
//  Created by Chen Jing on 15/8/4.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDShareViewController.h"
//#import "WeiboSDK.h"
//#import "WXApiObject.h"
#import "DDisBlankString.h"
#import "UIImageView+WebCache.h"

#define kRedirectURI    @"https://api.weibo.com/oauth2/default.html"
#define kBody @"http://app.dd5s.com:20010/dingdangv2-front/front/share.jhtml?qId="//专业的求解答疑应用，在线解答过程，思路真人语音讲解，让你学会学习。

@interface DDShareViewController ()

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property(nonatomic,strong)UIImage * snapshotImage;
@property(nonatomic,assign)UIViewController * tempVc;
@property(nonatomic,strong)UIView * shadowView;

@end

@implementation DDShareViewController


//+(void)showShareWithController:(UIViewController *)vc withQid:(NSString *)qid title:(NSString *)title qBodyText:(NSString *)text
//{
//    DDShareViewController * shareVC = [[DDShareViewController alloc]init];
//    
//    shareVC.accept_qid = qid;
//    shareVC.accept_title = title;
//    shareVC.accept_qBodyText = text;
//    
//    shareVC.tempVc = vc;
//    
//    shareVC.shadowView = [[UIView alloc]initWithFrame:vc.view.bounds];
//    shareVC.shadowView.backgroundColor = [UIColor blackColor];
//    shareVC.shadowView.alpha = .3;
//    [vc.view addSubview:shareVC.shadowView];
//    
//    
//    shareVC.view.frame = CGRectMake(0,CGRectGetHeight(vc.view.bounds) , CGRectGetWidth([[UIScreen mainScreen] bounds]), 220);
//    
//    [vc addChildViewController:shareVC];
//    [vc.view addSubview:shareVC.view];
//
//    [shareVC didMoveToParentViewController:vc];
//    UIGraphicsBeginImageContextWithOptions(vc.view.bounds.size, NO, [UIScreen mainScreen].scale);
//    [vc.view drawViewHierarchyInRect:vc.view.bounds afterScreenUpdates:YES];
//    shareVC.snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    [shareVC showShare];
//
//}

- (void)viewDidLoad {
    NSLog(@"进入到---------->DDShareViewController");
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.cancelBtn.layer.cornerRadius = 5;
    self.cancelBtn.layer.masksToBounds = YES;
    self.cancelBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.cancelBtn.layer.borderWidth = .5;
    
    
    //是否安装了qq
    //_qqLoginBtn.hidden = ![QQApiInterface isQQInstalled];
    //是否安装了微博
    //_weiboLoginBtn.hidden = ![WeiboSDK isWeiboAppInstalled];
    //是否安装了微信
    //_weixinLoginBtn.hidden = ![WXApi isWXAppInstalled];
    

}

//-(void)showShare{
//    
//    [UIView animateWithDuration:.35 animations:^{
//        self.view.frame = CGRectMake(0, CGRectGetHeight(self.tempVc.view.bounds) - 220, CGRectGetWidth([[UIScreen mainScreen] bounds]), 220);
//    }];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (IBAction)btn_sinaWeibo:(id)sender {
//    
//    [self cancel:nil];
//
////    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
////    authRequest.redirectURI = kRedirectURI;
////    authRequest.scope = @"all";
////
////    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:WEIXIN_AppID];
////    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
////                         @"Other_Info_1": [NSNumber numberWithInt:123],
////                         @"Other_Info_2": @[@"obj1", @"obj2"],
////                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
////    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
////    [WeiboSDK sendRequest:request];
//    
//    
//    AppDelegate *myDelegate =RYAppDelegate;//(AppDelegate*)[[UIApplication sharedApplication] delegate];
//    NSString *url = [kBody stringByAppendingString:_accept_qid];
//    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
//    authRequest.redirectURI =url;
//    authRequest.scope =@"all";
//    
//    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:nil];
//    request.userInfo =@{@"ShareMessageFrom":@"ChatRootViewController",
//                        @"Other_Info_1": [NSNumber numberWithInt:123],
//                        @"Other_Info_2": @[@"obj1",@"obj2"],
//                        @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
//    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
//    [WeiboSDK sendRequest:request];
//    
//    
//    
//}
//
//- (WBMessageObject *)messageToShare
//{
//    WBMessageObject *message = [WBMessageObject message];
//    
//    //    if (self.textSwitch.on)
//    //    {
//    //        message.text = NSLocalizedString(@"测试通过WeiboSDK发送文字到微博!", nil);
//    //    }
//    //
//    //    if (self.imageSwitch.on)
//    //    {
//    //        WBImageObject *image = [WBImageObject object];
//    //        image.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]];
//    //        message.imageObject = image;
//    //    }
//    //
//    //    if (self.mediaSwitch.on)
//    //    {
////    WBWebpageObject *webpage = [WBWebpageObjectobject];
////    webpage.objectID =@"identifier1";
////    webpage.title =NSLocalizedString(@"分享网页标题wwww",nil);
////    webpage.description = [NSStringstringWithFormat:NSLocalizedString(@"测试来自你的应用名称-%.0f",nil), [[NSDatedate] timeIntervalSince1970]];
////    webpage.thumbnailData = [NSDatadataWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"生气"ofType:@"png"]];
////    webpage.webpageUrl =@"http://www.baidu.com";
////    message.mediaObject = webpage;
//    //    }
//    
//    return message;
//}
//- (IBAction)qq:(id)sender {
//    
//    [self cancel:nil];
//    TencentOAuth *tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQ_AppId andDelegate:self];
//    DDSharedUser *userInfo = [DDSharedUser shared];
//    NSString *url = [kBody stringByAppendingString:_accept_qid];
//    NSString *description;
//    if(self.accept_qBodyText.length>0){
//        description = self.accept_qBodyText;
//    }else{
//        description = self.accept_title;
//    }
//    
//    QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:self.accept_title description:description previewImageURL:[NSURL URLWithString:userInfo.userInfo.avatarUrl]];
//    //[newsObj setCflag:[self shareControlFlags]];
//    
//    //SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
//    //QQApiSendResultCode sent =0;
//    //sent = [QQApiInterfaceSendReqToQZone:req];
//        //sent = [QQApiInterface sendReq:req];//这是分享到QQ
//    //[self handleSendResult:sent];//这是分享到空间
//    
//    
//
//    
////    QQApiTextObject* txtObj = [QQApiTextObject objectWithText:[kBody stringByAppendingString:_accept_qid]];
////    
////    txtObj.title = [NSString stringWithFormat:@"我是%@%@,我在丁丁当当看到好晒晒，分享一下！",userInfo.userInfo.school,userInfo.userInfo.nickName];
////    
////    if(self.accept_qBodyText.length>0){
////        txtObj.description = self.accept_qBodyText;
////    }else{
////        txtObj.description = txtObj.title;
////    }
// 
//    
//    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:newsObj];
//
//    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
//    [self handleSendResult:sent];
//}

//- (IBAction)weixin:(id)sender {
//    
//    [self cancel:nil];
//    
//  
//    WXWebpageObject *ext = [WXWebpageObject object];
//    ext.webpageUrl = [kBody stringByAppendingString:_accept_qid];
//    
//    
//    WXMediaMessage *message = [WXMediaMessage message];
//    DDSharedUser *userInfo = [DDSharedUser shared];
//    
//    message.title = [NSString stringWithFormat:@"我是%@%@,我在丁丁当当看到好晒晒，分享一下！",userInfo.userInfo.school,userInfo.userInfo.nickName];//@"丁当平台 - 大学生互动平台";
//    
//    if(self.accept_qBodyText.length>0){
//        message.description = self.accept_qBodyText;
//    }else{
//        message.description = message.title;
//    }
//        
//    
//    [message setThumbImage:[UIImage imageNamed:@"icon-120.png"]];//缩略图片
//    
////    if ([DDisBlankString isBlankString:userInfo.userInfo.avatarUrl]){
////        [message setThumbImage:[UIImage imageNamed:@"icon-180"]];//缩略图片
////    }else{
////
////        UIImage *userHeaderImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userInfo.userInfo.avatarUrl]]];
////        message.thumbData = UIImageJPEGRepresentation(userHeaderImage, 1);
//    
////        NSURL *url = [NSURL URLWithString:userInfo.userInfo.avatarUrl];
////        
////        [userHeaderImage sd_setImageWithURL:url];
//        //[message setThumbImage:userHeaderImage];//缩略图片
//    //}
//    message.mediaObject = ext;
//    
//    //SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
//    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
//    req.bText = NO;
//    req.message = message;
//    req.scene = WXSceneSession;
//    
//    [WXApi sendReq:req];
//    
//    
//
//}
- (IBAction)cancel:(id)sender {
    [UIView animateWithDuration:.35 animations:^{
        self.view.frame = CGRectMake(0, CGRectGetHeight([[UIScreen mainScreen] bounds]), CGRectGetWidth([[UIScreen mainScreen] bounds]), 220);
    }];
    [self.shadowView removeFromSuperview];
}



//- (void)handleSendResult:(QQApiSendResultCode)sendResult
//{
//    switch (sendResult)
//    {
//        case EQQAPIAPPNOTREGISTED:
//        {
//            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [msgbox show];
//
//            break;
//        }
//        case EQQAPIMESSAGECONTENTINVALID:
//        case EQQAPIMESSAGECONTENTNULL:
//        case EQQAPIMESSAGETYPEINVALID:
//        {
//            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [msgbox show];
//
//            break;
//        }
//        case EQQAPIQQNOTINSTALLED:
//        {
//            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [msgbox show];
//
//            break;
//        }
//        case EQQAPIQQNOTSUPPORTAPI:
//        {
//            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [msgbox show];
//
//            break;
//        }
//        case EQQAPISENDFAILD:
//        {
//            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [msgbox show];
//
//            break;
//        }
//        default:
//        {
//            break;
//        }
//    }
//}


#pragma mark -
#pragma Internal Method

//- (WBMessageObject *)messageToShare{
//    WBMessageObject *message = [WBMessageObject message];
//    WBImageObject * tempImageObj = [WBImageObject object];
//    tempImageObj.imageData = UIImagePNGRepresentation(self.snapshotImage);
//    message.text = kBody;
//    message.imageObject = tempImageObj;
//    return message;
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
